﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace JavascriptWebResourcesManager
{
    class ToolSettings
    {
        private const string XPATH = "JavascriptWebResourcesManagerConfig/LastUsedFolder";

        private XmlDocument settingsDoc;

        public string FolderPath
        {
            get {
                if (this.settingsDoc == null)
                    this.LoadXmlDocument();

                if (this.settingsDoc == null || this.settingsDoc.SelectSingleNode(XPATH) == null)
                    return null;
                else
                    return this.settingsDoc.SelectSingleNode(XPATH).InnerText;
            }
            set {
                if (this.settingsDoc == null)
                    this.CreateXmlDocument();
                
                    this.settingsDoc.SelectSingleNode(XPATH).InnerText = value;

                this.settingsDoc.Save("JavascriptWebResourcesManager.xml");
            }
        }

        private void LoadXmlDocument()
        {
            this.settingsDoc = new XmlDocument();

            try
            {
                this.settingsDoc.Load("JavascriptWebResourcesManager.xml");
            }
            catch
            {
                this.settingsDoc = null;
            }
        }

        private void CreateXmlDocument()
        {
            this.settingsDoc = new XmlDocument();
            XmlNode rootNode = this.settingsDoc.CreateElement("JavascriptWebResourcesManagerConfig");
            XmlNode pathNode = this.settingsDoc.CreateElement("LastUsedFolder");

            rootNode.AppendChild(pathNode);
            this.settingsDoc.AppendChild(rootNode);
        }
    }
}
